package cn.shop.admin.controller;

import cn.hutool.core.util.StrUtil;
import cn.shop.admin.req.RecordQueryReq;
import cn.shop.admin.service.RecordService;
import cn.shop.base.MyPage;
import cn.shop.transcation.rechargerecord.entity.Record;
import cn.shop.utils.ApiResult;
import cn.shop.utils.EnumUtil;
import cn.shop.utils.enums.TransactionState;
import cn.shop.validation.Save;
import cn.shop.validation.Update;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * 充值记录(Record)表控制层
 *
 * @author jzw
 * @since 2021-12-06 14:42:50
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/record")
public class RecordController {

	/**
	 * 服务对象
	 */
	@Resource
	private RecordService recordService;

	/**
	 * 查询所有数据
	 *
	 * @param record 查询实体
	 * @return 所有数据
	 * @author jzw
	 * @since 2021-12-06 14:42:50
	 */
	@GetMapping("queryAll")
	public ApiResult<List<Record>> queryAll(Record record) {
		record.setState(TransactionState.COMPLETED);
		return ApiResult.success(recordService.list(new QueryWrapper<>(record)));
	}

	/**
	 * 分页查询所有数据
	 *
	 * @param req   查询对象
	 * @return 所有数据
	 * @author jzw
	 * @since 2021-12-06 14:42:50
	 */
	@GetMapping("queryByPage")
	public ApiResult<MyPage<Record>> queryByPage(RecordQueryReq req) throws InvocationTargetException, IllegalAccessException {
		log.info("查询条件：{}", req);
		MyPage<Record> page = MyPage.of(req.getCurrent(), req.getSize());
		QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(StrUtil.isNotBlank(req.getPlayerName()), "player_name", req.getPlayerName())
				.eq(req.getId() != null, "id", req.getId())
				.apply(req.getStartTime() != null,
						"create_time >={0}", req.getStartTime())
				.apply(req.getEndTime() != null,
						"create_time <= {0}", req.getEndTime());
		if (req.getState() == null) {
			queryWrapper.in("state", TransactionState.COMPLETED, TransactionState.MANUAL);
		} else {
			queryWrapper.eq("state", EnumUtil.getEnum(TransactionState.class, req.getState()));
		}
		return ApiResult.success(recordService.page(page, queryWrapper));
	}

	@GetMapping("statics")
	public ApiResult<Record> statics(RecordQueryReq req) {
		QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("ifnull(sum(amount), 0) amount");
		queryWrapper
				.eq(StrUtil.isNotBlank(req.getPlayerName()), "player_name", req.getPlayerName())
				.apply(req.getStartTime() != null,
						"create_time >={0}", req.getStartTime())
				.apply(req.getEndTime() != null,
						"create_time <= {0}", req.getEndTime());
		if (req.getState() == null) {
			queryWrapper.in("state", TransactionState.COMPLETED, TransactionState.MANUAL);
		} else {
			queryWrapper.eq("state", req.getState());
		}
		return ApiResult.success(recordService.getOne(queryWrapper));
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 * @author jzw
	 * @since 2021-12-06 14:42:50
	 */
	@GetMapping("queryOne/{id}")
	public ApiResult<Record> selectOne(@PathVariable Serializable id) {
		return ApiResult.success(recordService.getById(id));
	}

	/**
	 * 新增数据
	 *
	 * @param record 实体对象
	 * @return 新增结果
	 * @author jzw
	 * @since 2021-12-06 14:42:50
	 */
	@PostMapping("insert")
	public ApiResult<Map<String, Object>> insert(@Validated(Save.class) @RequestBody Record record) {
		return ApiResult.result(recordService.save(record));
	}

	/**
	 * 修改数据
	 *
	 * @param record 实体对象
	 * @return 修改结果
	 * @author jzw
	 * @since 2021-12-06 14:42:50
	 */
	@PutMapping("update")
	public ApiResult<Map<String, Object>> update(@Validated(Update.class) @RequestBody Record record) {
		return ApiResult.result(recordService.updateById(record));
	}

	/**
	 * 删除单条数据
	 *
	 * @param id 主键
	 * @return 删除结果
	 * @author jzw
	 * @since 2021-12-06 14:42:50
	 */
	@DeleteMapping("delete/{id}")
	public ApiResult<Map<String, Object>> delete(@PathVariable Long id) {
		Record record = new Record();
		record.setId(id);
		return ApiResult.result(recordService.removeById(record));
	}


}

